
# This file generates Figure 2: Party Closeness and Reported Importance of Scandal for Voting Decisions

# R 3.2.2
library(foreign) # v 0.8-66
library(ggplot2) # v 1.0.1
library(splines) # v 3.2.2
library(gridExtra) # v 2.0.0
# may have to install Arial font
library(extrafont) # v 0.17
# font_import(pattern=".*ria.*") # font_import(pattern="Arial") doesn't work!?
#loadfonts(device="win")

# set directory here (the folder where the subfolders are located)
# setwd(...)

d <- read.dta("survey/medw_out_forgraph.dta")

# drop cases with inconsistent residence info in raw data (N=70, 64 of which are part of two groups as defined below) 
d <- subset(d,wk_inconsistent != 1) 

# make separate datasets
dtr <- subset(d,affair_run == 1) # re-running implicated MP in SMD
dct <- subset(d,affair_run == 0 & affair_norun == 0) # no re-running and no stepped-down MP in SMD

# additional variable for re-positioning scatter
dtr$affair_vimp_alt <- dtr$affair_vimp
dtr$affair_vimp_alt[dtr$affair_vimp_alt==0] <- -.15
dtr$affair_vimp_alt[dtr$affair_vimp_alt==1] <- .65

dct$affair_vimp_alt <- dct$affair_vimp
dct$affair_vimp_alt[dct$affair_vimp_alt==0] <- -.15
dct$affair_vimp_alt[dct$affair_vimp_alt==1] <- .65


dtr.vint <- subset(d,affair_run == 1 & vintpol == 1)
dct.vint <- subset(d,affair_run == 0 & affair_norun == 0 & vintpol == 1)

# additional variable for re-positioning scatter
dtr.vint$affair_vimp_alt <- dtr.vint$affair_vimp
dtr.vint$affair_vimp_alt[dtr.vint$affair_vimp_alt==0] <- -.15
dtr.vint$affair_vimp_alt[dtr.vint$affair_vimp_alt==1] <- .65

dct.vint$affair_vimp_alt <- dct.vint$affair_vimp
dct.vint$affair_vimp_alt[dct.vint$affair_vimp_alt==0] <- -.15
dct.vint$affair_vimp_alt[dct.vint$affair_vimp_alt==1] <- .65



# with jittered scatter, alternative pos
myplot1 <- ggplot() +
  geom_jitter(data = dct, aes(x = prob_second,y = affair_vimp_alt),
              position = position_jitter(w=0,h=.1),
              colour = 'grey65',shape=4,alpha=.5, size=1.75) +
  geom_jitter(data = dtr, aes(x = prob_second,y = affair_vimp_alt),
              position = position_jitter(w=0,h=.1),
              colour = 'grey30',shape=16,alpha = 0.5, size=1.75) +
  stat_smooth(data = dtr, aes(x = prob_second,y = affair_vimp),method="loess",n=100,
              fill="grey80", colour="black", size=.8) +
  stat_smooth(data = dct, aes(x = prob_second,y = affair_vimp),
              method="loess",n=100,
              fill="grey40", colour="grey20", linetype=2, size=.8) +
  geom_hline(aes(yintercept=.52),linetype=3) +
  geom_hline(aes(yintercept=-.02),linetype=3) +
  labs(title = "All respondents",
       x="'Closeness' to CSU \n (Expected probability of CSU second vote)",
       y="Affair very imp. for voting decisions 0/1") +
  scale_x_continuous(breaks=seq(0,1,.1)) +
  scale_y_continuous(breaks=seq(0,.5,.1)) +
  theme_bw(base_family="Arial") +
  theme(title = element_text(size = rel(0.6))) +
  theme(axis.text = element_text(size = rel(0.55)))



myplot2 <- ggplot() +
  geom_jitter(data = dct.vint, aes(x = prob_second,y = affair_vimp_alt),
              position = position_jitter(w=0,h=.1),
              colour = 'grey65',shape=4,alpha=.5, size=1.75)  +
  geom_jitter(data = dtr.vint, aes(x = prob_second,y = affair_vimp_alt),
              position = position_jitter(w=0,h=.1),
              colour = 'grey30',shape=16,alpha = .5, size =1.75) +
  stat_smooth(data = dtr.vint, aes(x = prob_second,y = affair_vimp),method="loess",n=100,
              fill="grey80", colour="black", size=.8) +
  stat_smooth(data = dct.vint, aes(x = prob_second,y = affair_vimp),
              method="loess",n=100,
              fill="grey40", colour="grey20", linetype=2, size=.8) +
  geom_hline(aes(yintercept=.52),linetype=3) +
  geom_hline(aes(yintercept=-.02),linetype=3) +
  labs(title = "Respondents with strong political interest",
       x="'Closeness' to CSU \n (Expected probability of CSU second vote)",
       y="Affair very imp. for voting decisions 0/1") + 
  scale_x_continuous(breaks=seq(0,1,.1)) +
  scale_y_continuous(breaks=seq(0,.5,.1)) +
  theme_bw(base_family="Arial") + 
  theme(title = element_text(size = rel(0.6))) +
  theme(axis.text = element_text(size = rel(0.55)))




graphics.off()
tiff("survey/figure2.tif",width=1800,height=1200, res=300, compression="zip+p")
grid.arrange(myplot1, myplot2, ncol=2)
dev.off()




